<?php

require_once BaseUtil::getModulePath("base")  .  '/forms/BaseForm.php';
require_once   BaseUtil::getModulePath("base")  .  '/validate/Unique.php';

class CategoryForm extends BaseForm {
    
    public function __construct ($metadataSets=array(),$category=array())
    {
        parent::__construct();
        if(empty($category)){
           	$this->setAction(HttpUtil::getUrl("category", "subadd"));
        }else{
            $this->setAction(HttpUtil::getUrl("category", "subedit"));
        }

        $categoryId = new Zend_Form_Element_Hidden("categoryId");
        $this->addElement($categoryId);
        if(isset($category['id'])){           
            $categoryId->setValue($category['id']);        
        }
        
        
        // shortname
        $shortName = new Zend_Form_Element_Text('shortName');
        $shortName->setLabel($this->translate->_('shortName'));
        $shortName->setAttrib("id", "shortName");
        $shortName->setAttrib("onblur", "javascript:Inventory.copyShortName();");
        $shortName->addFilter("StringTrim");
        $shortName->setRequired(true);
        if(isset($category['short_name'])){
            $shortName->setValue($category['short_name']);
        }

        // fullname
        $fullName = new Zend_Form_Element_Text('fullName');
        $fullName->setLabel($this->translate->_('fullName'));
        $fullName->setAttrib("id", "fullName");
        $fullName->addFilter("StringTrim");
        $fullName->setRequired(true);
        if(isset($category['full_name'])){
            $fullName->setValue($category['full_name']);
        }
        //parent
        $parent = new Zend_Form_Element_Hidden('parent');
        $parent->setValue("");
        $parent->setRequired(true);
        $parent->addFilter("StringTrim");
        if(isset($category['parent_id'])){
            $parent->setValue($category['parent_id']);
        }
        //visiable
        $visible = new Zend_Form_Element_Select('visible');
        $visible->setLabel($this->translate->_('visibleTitle'));
        $visible->setMultiOptions(array("1"=> $this->translate->_('visible') ,"0"=> $this->translate->_('invisible')));
        $this->addElement($visible);
        if(isset($category['visible'])){
            $visible->setValue($category['visible']);
        }
        
        //sortOrder
        $sortOrder = new Zend_Form_Element_Text('sortOrder');
        $sortOrder->setLabel($this->translate->_('sortOrder'));
        $sortOrder->addFilter("StringTrim");
        if(isset($category['sort_order'])){
            $sortOrder->setValue($category['sort_order']);
        }
        //urlTitle
        $urlTitle = new Zend_Form_Element_Text('urlTitle');
        $urlTitle->setLabel($this->translate->_('urlTitle'));
        $urlTitle->setAttrib("id", "urlTitle");
        $urlTitle->addFilter("StringTrim");
        $urlTitle->setRequired(true);
	$validUniqueUrl = new MyValid_Unique();
        if(isset($category['id'])){
            $validUniqueUrl->setFetchAttr('categories', 'url_title', $category['id']);
        }else{
            $validUniqueUrl->setFetchAttr('categories', 'url_title');
        }
        $urlTitle->addValidator($validUniqueUrl);
        if(isset($category['url_title'])){
            $urlTitle->setValue($category['url_title']);
        }
        //seoTitle
        $seoTitle = new Zend_Form_Element_Text('seoTitle');
        $seoTitle->setLabel($this->translate->_('seoTitle'));
        $seoTitle->addFilter("StringTrim");
        if(isset($category['seo_title'])){
            $seoTitle->setValue($category['seo_title']);
        }
        //seoKeyWord
        $seoKeyWord = new Zend_Form_Element_Text('seoKeyWord');
        $seoKeyWord->setLabel($this->translate->_('seoKeyWord'));
        $seoKeyWord->addFilter("StringTrim");
        if(isset($category['seo_keyword'])){
            $seoKeyWord->setValue($category['seo_keyword']);
        }
        //seoDescription
        $seoDescription = new Zend_Form_Element_Textarea('seoDescription');
        $seoDescription->setLabel($this->translate->_('seoDescription'));
        $seoDescription->setAttribs(array("rows" => 5, "cols" => 40));
        $seoDescription->addFilter("StringTrim");
        if(isset($category['seo_description'])){
            $seoDescription->setValue($category['seo_description']);
        }

        //metadata set
        $metadataSet = new Zend_Form_Element_Select('metadataSet');
        $metadataSet->setLabel($this->translate->_('metadataSet'));
        $metadataSet->setMultiOptions($metadataSets);
        //$metadataSet->setRequired(true);
        if(isset($category['metadata_set_id'])){
            $metadataSet->setValue($category['metadata_set_id']);
        }
        
        //price section
        $priceSection = new Zend_Form_Element_Textarea('priceSection');
        $priceSection->setLabel($this->translate->_('priceSection'));
        $priceSection->setAttribs(array("rows" => 3, "cols" => 40));
        $priceSection->addFilter("StringTrim");
        if(isset($category['price_section'])){
            $priceSection->setValue($category['price_section']);
        }

        $this->addElements(array($shortName,$fullName,$parent,$sortOrder,$urlTitle,$seoTitle,$seoKeyWord,$seoDescription,$metadataSet,$priceSection));

        $this->setElementDecorators(array(
            'ViewHelper',
            array(array('div' => 'HtmlTag'), array('tag' => 'div',"class" => "form_element")),
        ));

    }
    
}